//
//  CLDocImageCell.m
//  chanlin
//
//  Created by Ryan on 14-11-18.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLDocImageCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Utils.h"
#import "IFMoviePlayerController.h"
#import "BlockUI.h"

@implementation CLDocImageCell

- (void)initUI{
    self.docImageView=[[UIImageView alloc] init];
    self.docImageView.userInteractionEnabled = YES;
    self.docImageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.docImageView];
    
    self.noImageButton=[UIButton buttonWithType:UIButtonTypeCustom];
    _noImageButton.frame = CGRectMake(0, 0, 84+40, 28+40);
    _noImageButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    _noImageButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    _noImageButton.backgroundColor = [UIColor clearColor];
    _noImageButton.contentEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
    _noImageButton.hidden=YES;
    [_noImageButton setTitle:@"点击下载图片" forState:UIControlStateNormal];
    [_noImageButton setTitleColor:RGB(0x2b, 0x54, 0x70) forState:UIControlStateNormal];
    [_noImageButton addTarget:self action:@selector(didSelectedNoImageButton) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_noImageButton];
    
    _downloadLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 84, 28)];
    _downloadLabel.text = @"正在载入...";
    _downloadLabel.backgroundColor = [UIColor clearColor];
    _downloadLabel.textAlignment = NSTextAlignmentCenter;
    _downloadLabel.font = [UIFont systemFontOfSize:14.0];
    _downloadLabel.textColor = RGB(0x2b, 0x54, 0x70);
    [self addSubview:_downloadLabel];
    _downloadLabel.hidden = YES;
    
    self.backgroundColor=RGB(0xf8, 0xf8, 0xf8);
    
    self.note=[[IFLabel alloc] init];
    [self.contentView addSubview:self.note];
//    self.note=[[UITextView alloc] init];
//    self.note.contentInset=UIEdgeInsetsMake(-10.f, -4.f, 0, 0);
//    self.note.scrollEnabled=NO;
//    self.note.editable=NO;
//    self.note.backgroundColor=self.backgroundColor;
//    // self.text.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
//    [self.contentView addSubview:self.note];
}

- (void)didSelectedNoImageButton{
    if (_downloadLabel.hidden) {
        _downloadLabel.hidden = NO;
        _noImageButton.hidden = YES;
    }
    __weak typeof(self) me=self;
    UIImage *image = [UIImage imageNamed:@"doc_ default_image.png"];
    [self.docImageView sd_setImageWithURL:[NSURL URLWithString:self.url]
                      placeholderImage:image
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 if(!image){
                                     me.downloadLabel.hidden = YES;
                                     me.noImageButton.hidden = NO;
                                     
                                 }
                             }];

}

- (void)loadElement:(INHtmlElement *)element{
    self.url=element.strValue;
    
    self.docImageView.frame=CGRectMake(I_DOC_TEXT_MARGIN_SPACE, 22.f, I_DOC_CONTENT_WIDTH, element.imgHeight);
    if(_isStrNULL(self.url)){
        self.docImageView.image=nil;
        return;
    }
    __weak typeof(self) me=self;
   //  NSLog(@"%@",self.url);
    UIImage *image = [UIImage imageNamed:@"doc_ default_image.png"];
    [self.docImageView sd_setImageWithURL:[NSURL URLWithString:self.url]
                      placeholderImage:image
                             completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                 if(!image){
                                     me.downloadLabel.hidden = YES;
                                     me.noImageButton.hidden = NO;

                                 }
    }];
   
    self.note.frame=CGRectMake(I_DOC_TEXT_MARGIN_SPACE, self.docImageView.bottom+6, I_DOC_CONTENT_WIDTH,element.noteHeight+12.f);
    self.note.attributStr=element.noteAttr;
    [self.note setNeedsDisplay];
    
}

- (void)setIsSlider:(BOOL)isSlide itemId:(NSString *)itemId{
    self.itemId=itemId;
    if(isSlide){
        self.iconImageView=[[UIImageView alloc] init];
        self.iconImageView.frame=CGRectMake((I_DOC_CONTENT_WIDTH)-42.0f, CGRectGetHeight(self.docImageView.frame)-22.0f, 42.0f, 22.0f);
        self.iconImageView.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleTopMargin;
        self.iconImageView.image=[UIImage imageNamed:@"doc_AtlasLabel.png"];
        [self.docImageView addSubview:self.iconImageView];

    }else{
        [self.iconImageView removeFromSuperview];
        self.iconImageView=nil;
    }
}

- (void)dealloc{
    if(self.pc){
        [[CLGloble sharedInstance] clearMovie];
    }
}

- (void)setIsVideo:(BOOL)isvideo url:(NSString *)url{
    self.videourl=url;
    if(isvideo){
        if(!self.play){
            self.play=[UIButton buttonWithType:UIButtonTypeCustom];
            self.play.frame=CGRectMake(0, 0, 76, 76);
            [self.play setImage:[UIImage imageNamed:@"doc_playImage.png"] forState:UIControlStateNormal];
            [self.play setCenter:CGPointMake(self.docImageView.width/2.f, self.docImageView.height/2.f)];
            [self.docImageView addSubview:self.play];
            
            __weak typeof(self) me=self;
            [self.play handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
                if (me.url&& [me.url length] > 4){
                    [[CLGloble sharedInstance] clearMovie];
                    CGRect frame=me.docImageView.frame;
                    IFMoviePlayerController *moviePlayer = [[IFMoviePlayerController alloc] initWithContentURL:nil WithType:IFVideo_Type_Document];
                    //self.moviePlayer.delegate = self;
                    //self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
                    me.pc=moviePlayer;
                    [moviePlayer.view setFrame:frame];
                    
                    //IEmbedMoviePlayer *player=[[IEmbedMoviePlayer alloc] initWithFrame:frame];
                    //        IEmbedMoviePlayer *player =[IEmbedMoviePlayer getEmbedMoviePlayer:frame];
                    //              [player setframe:frame];
                    //        player.playerController.videoIdStr = self.documentId;
                    //        self.moviePlayer = player;
                    [me addSubview:moviePlayer.view];
                    
                    [moviePlayer setContentURL:[NSURL URLWithString:self.videourl]];
                    [moviePlayer prepareToPlay];
                    [moviePlayer play];
                    [CLGloble sharedInstance].singleMovie=moviePlayer;
                    
                    
                }
            }];
        }
    }else{
        if(self.play){
            [self.play removeFromSuperview];
            self.play=nil;
        }
    }
    
    
}

@end
